﻿using System;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using TradeMeSDK;

namespace TradeMo
{
    public class BuyingViewModel : BaseViewModel
    {
        private ObservableCollection<SearchResultItem> _watchList;
        public ObservableCollection<SearchResultItem> WatchList
        {
            get { return _watchList ?? (_watchList = new ObservableCollection<SearchResultItem>()); }
        }

        private ObservableCollection<SearchResultItem> _wonList;
        public ObservableCollection<SearchResultItem> WonList
        {
            get { return _wonList ?? (_wonList = new ObservableCollection<SearchResultItem>()); }
        }

        private ObservableCollection<SearchResultItem> _lostList;
        public ObservableCollection<SearchResultItem> LostList
        {
            get { return _lostList ?? (_lostList = new ObservableCollection<SearchResultItem>()); }
        }


        private int _loadingCount;
        public bool IsLoading
        {
            get { return _loadingCount != 0; }
        }
        private void IncreaseLoadingCount()
        {
            _loadingCount++;
            NotifyPropertyChanged("IsLoading");
        }
        private void DecreaseLoadingCount()
        {
            _loadingCount--;
            NotifyPropertyChanged("IsLoading");
        }

        public void LoadWatchList()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeWatchlist(criteria, result =>
                {
                    DecreaseLoadingCount();
                    if (result.IsError)
                    {
                        MessageBox.Show("Bugger: Unable to load watchlist");
                        return;
                    }

                    WatchList.Clear();

                    foreach (var listing in result.Response.List)
                        WatchList.Add(SearchResultItem.Build(listing));
                });
        }

        public void LoadWonList()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeWonItems(criteria, result =>
            {
                DecreaseLoadingCount();
                if (result.IsError)
                {
                    MessageBox.Show("Bugger: Unable to load won list");
                    return;
                }

                WonList.Clear();
                foreach (var listing in result.Response.List)
                    WonList.Add(SearchResultItem.Build(listing));
            });
        }

        public void LoadLostList()
        {
            IncreaseLoadingCount();
            MyTradeMeCriteria criteria = new MyTradeMeCriteria();
            App.TradeMe.MyTradeMe.MyTradeMeLostItems(criteria, result =>
            {
                DecreaseLoadingCount();
                if (result.IsError)
                {
                    MessageBox.Show("Bugger: Unable to load lost list");
                    return;
                }

                LostList.Clear();
                foreach (var listing in result.Response.List)
                    LostList.Add(SearchResultItem.Build(listing));
            });
        }
    }
}
